package com.antgroup.seckill.business.rocketmq;

import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;

/**
 * @author wangxy
 * @date 2024/8/11 20:08
 */
@Service
public class RocketmqTransactionProducer {

    @Autowired
    private RocketMQTemplate rocketMQTemplate;




    public void sendMessageTransaction(String topic, String message) throws InterruptedException {

        String [] tags = new String [] {"TagA","TagB","TagC","TagD","TagE"};

        for (int i = 0; i < 10; i++) {
            Message<String> msg = MessageBuilder.withPayload(message).build();

            //topic与tag整合在一起
            String destination = topic + ":" + tags[i % tags.length];

            TransactionSendResult sendResult = rocketMQTemplate.sendMessageInTransaction(destination, msg, destination);
            System.out.println(sendResult);
            Thread.sleep(10);
        }

    }


}
